//统计
const stack = [];
let userInfo = null;
let unsubscribe = null;
const setTrack = function (obj) {

  mixpanel.track(window.location.host,{...userInfo, module: obj.mpModule});
}

export default {
  track(ns, re, cb) {
    if(userInfo) {
      setTrack(this);
    }else {
      stack.push(this);
      if(!unsubscribe) {
        const unsubscribe = store.subscribe(() => {
          const date = store.getState().app;
          if(date.profile&&date.profile.orgId && date.org){
            userInfo = {
              orgName:date.org.fullName,
              userId:date.profile.userId,
              role:(date.profile.roleNameList || []).join(','),
            };
            unsubscribe();
            let length = stack.length;
            while(~--length) {
              setTrack(stack.pop());
            }
          }
        })
      }
    }
    cb && cb();
  },
  trackOkrList(type) {
    const mpModule = `目标::${type==1 ? '公司' : type == 2 ? '团队' : '个人'}目标列表`;
    this.track && this.track.call({mpModule});
  }
}
